package com.dhq.myapp.util

import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import kotlin.math.abs


/**
 * DESC
 * Created by admin on 2018/12/11.
 */
object DateUtils {
    private val DF_SSS: DateFormat =
        SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault())
    private val DF: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())


    val currentDate: String
        get() = getCurrentDate(1)

    /**
     * 获取系统当前时间
     *
     * @param dateType 时间格式
     * @return
     */
    fun getCurrentDate(dateType: Int): String {
        val simpleDateFormat = SimpleDateFormat(getDateType(dateType)) // HH:mm:ss
        //获取当前时间
        val date = Date(System.currentTimeMillis())
        val timeStr = simpleDateFormat.format(date)
        return timeStr
    }


    /**
     * @param time
     * @param timeType
     * @return
     */
    fun getDateArr(time: String?, timeType: Int): Array<String?> {
        val arr = arrayOf<String?>("", "", "")

        try {
            val format = SimpleDateFormat(getDateType(timeType))
            val date = format.parse(time)
            val format2 = SimpleDateFormat(getDateType(1))
            val formatStr = format2.format(date)
            val arrSplit: Array<String?> =
                formatStr.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            if (arrSplit.size >= 3) {
                for (i in arr.indices) {
                    arr[i] = arrSplit[i]
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }


        return arr
    }


    /**
     * 获取日期字符串格式
     *
     * @param date
     * @return
     */
    fun getDateStr(date: Date?): String {
        //可根据需要自行截取数据显示
//        Log.d("getTime()", "choice date millis: " + date.getTime());
        val format = SimpleDateFormat(getDateType(1))
        return format.format(date)
    }


    /**
     * 获取时间格式字符串
     *
     * @param time     时间（秒）
     * @param dateType
     * @return
     */
    fun getDateStr(time: String, dateType: Int): String {
        val df = SimpleDateFormat(getDateType(dateType))
        val date = Date(time.toLong() * 1000)
        val formatStr = df.format(date)
        return formatStr
    }


    /**
     * 获取日期字符串格式
     *
     * @param date
     * @param dateType
     * @return
     */
    fun getDateStr(date: Date?, dateType: Int): String {
        //可根据需要自行截取数据显示
//        Log.d("getTime()", "choice date millis: " + date.getTime());
        val format = SimpleDateFormat(getDateType(dateType))
        return format.format(date)
    }


    /**
     * 获取日期字符串格式
     *
     * @param date         日期字符串
     * @param fromDateType 目前的字符串格式
     * @param toDateType   需要的字符串格式
     * @return
     */
    fun getDateStr(date: String?, fromDateType: Int, toDateType: Int): String {
        //可根据需要自行截取数据显示

        var toDate = ""

        try {
            val fromFormat = SimpleDateFormat(getDateType(fromDateType))
            val fromDate = fromFormat.parse(date)
            val toformat = SimpleDateFormat(getDateType(toDateType))
            toDate = toformat.format(fromDate)
            return toDate
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return toDate
    }


    private fun getDateType(dateType: Int): String {
        when (dateType) {
            1 -> return "yyyy-MM-dd"
            2 -> return "yyyy-MM-dd HH:mm:ss"
            3 -> return "yyyy-MM-dd HH:mm"
            4 -> return "HH:mm"
            5 -> return "HH:mm:ss"
            6 -> return "MM月dd日"
            7 -> return "yyyyMMdd"
            else -> return "yyyy-MM-dd"
        }
    }

    val currentWeekDay: String
        get() = getWeekDay(currentDate)


    /**
     * 根据当前日期获得是星期几
     *
     * @param dateStr  yyyy-MM-dd
     * @return
     */
    fun getWeekDay(dateStr: String?): String {
        val fromFormat = SimpleDateFormat(getDateType(1))
        var date: Date? = null
        try {
            date = fromFormat.parse(dateStr)
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        var Week = ""
        val c = Calendar.getInstance()
        c.setTime(date)

        val wek = c.get(Calendar.DAY_OF_WEEK)

        if (wek == 1) {
            Week += "星期日"
        }
        if (wek == 2) {
            Week += "星期一"
        }
        if (wek == 3) {
            Week += "星期二"
        }
        if (wek == 4) {
            Week += "星期三"
        }
        if (wek == 5) {
            Week += "星期四"
        }
        if (wek == 6) {
            Week += "星期五"
        }
        if (wek == 7) {
            Week += "星期六"
        }
        return Week
    }


    /**
     * 获取跟现在时间的的时间差  （返回毫秒差值）
     * @param firstDateStr
     * @param dateType
     * @return
     */
    fun getCurrentTimeDiff(firstDateStr: String?, dateType: Int): Long {
        return getTimeDiff(firstDateStr, dateType, getCurrentDate(2), 2)
    }

    /**
     * 获取两个时间的时间差  （返回毫秒差值）
     * @param firstDateStr
     * @param twoDateStr
     * @param dateType
     * @return
     */
    fun getTimeDiff(firstDateStr: String?, twoDateStr: String?, dateType: Int): Long {
        return getTimeDiff(firstDateStr, dateType, twoDateStr, dateType)
    }

    /**
     * 获取两个时间的时间差  （返回毫秒差值）
     * @param firstDateStr
     * @param firstDateType
     * @param twoDateStr
     * @param twoDateType
     * @return
     */
    fun getTimeDiff(
        firstDateStr: String?,
        firstDateType: Int,
        twoDateStr: String?,
        twoDateType: Int
    ): Long {
        val firstFormat = SimpleDateFormat(getDateType(firstDateType))
        val twoFormat = SimpleDateFormat(getDateType(twoDateType))
        try {
            val firstDate = firstFormat.parse(firstDateStr)
            val twoDate = twoFormat.parse(twoDateStr)
            val diff = abs(firstDate.getTime() - twoDate.getTime())
            return diff
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return 0
    }


    fun parseStr2Date(str: String): Date? {
        try {
            if (str.endsWith(".SSS")) {
                return DF_SSS.parse(str)
            } else {
                return DF.parse(str)
            }
        } catch (e: ParseException) {
            e.printStackTrace()
            return null
        }
    }

    fun formatDate2Str(date: Date?): String {
        return DF_SSS.format(date)
    }
}